package cn.newgrand.common;

import java.util.Random;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-10-13 下午04:40:32
 * 类说明
 */
public class StringUtil {
	public static boolean isEmpty(String s) {
	    if (s == null) {
	        return true;
	    }
	    
	    if (s.trim().length() == 0) {
	        return true;
	    }
	    
	    return false;
	}

	
	/**
	 * 新中大 加密算法 加密一个口令串 原理: 
	 * 1.随机生成一个长为aLen、由小写字母和数字组成的字符串;
	 * 2.第一个字符的ascii值减40为口令的偏移;
	 * 3.将口令转换为小写字母，每个字符间隔3位存放
	 * 4.最末几位顺序存放相应口令大小写情况，'1'-大写, '0'-小写 5.aLen至少大于26+字符数*4
	 * 
	 * alen 128位用于加密 用户的密码
	 * alen 1024为用于加密登入的URL
	 * 
	 * @param aPassword
	 * @param aLen
	 * @return
	 */
	
	public static String EncodeStringNG(String aPassword,int aLen)
	{
		char c;
		String sPassword="";
//		char[] achars = aPassword.toCharArray();
		int i,j,iOff,iLen;
		if (aPassword==null) return "";
//		iLen=aPassword.Length;
		iLen = aPassword.length();
		if (iLen==0) return "";

		//生成随机串
		Random rn=new Random();
		for (i=0;i<aLen-iLen-1;i++)
		{
			if (i==0)
			{
//				j=rn.Next(1,35);
				j = rn.nextInt(34)+1;
//				j= 26;
			}
			else 
			{
//				j=rn.Next(1,45);
				j = rn.nextInt(44)+1;
			}
			if (j>35)
			{	//从口令中取，目的：迷惑解密者
//				c=aPassword[rn.Next(1,iLen)-1];
				c = aPassword.charAt(rn.nextInt(iLen-1));
			}
			else if (j>25)
			{	//数字
//				c=System.Convert.ToChar(System.Convert.ToInt32('0')+j-26);
				//字符串0的ascll码为 48
				c = (char)(48+j-26);
			}
			else	
			{	//字母
//				c=System.Convert.ToChar(System.Convert.ToInt32('a')+j-1);
				c = (char)((int)('a')+j-1); 
			}
//			sPassword+=c.ToString();
			sPassword+=Character.toString(c);
		}

		//分隔符
//		sPassword+='a'.ToString();
		sPassword+=Character.toString('a');
		//根据大小写处理串尾
		for (i=0;i<iLen;i++)
		{
//			j=System.Convert.ToInt32(aPassword[i]);
			j = (int)aPassword.charAt(i);
//			if (j>=System.Convert.ToInt32('A') && j<=System.Convert.ToInt32('Z'))
			if(j>=(int)'A' && j<=(int)'Z')
			{
//				sPassword+='1'.ToString();
				sPassword+=Character.toString('1');
			}
			else
			{
//				sPassword+='0'.ToString();
				sPassword+=Character.toString('0');
			}
		}
		//偏移
//		iOff=System.Convert.ToInt32(sPassword[0])-40;
		iOff = (int)sPassword.charAt(0)-40;
		
		//拆
//		aPassword=aPassword.ToLower();
		aPassword = aPassword.toLowerCase();
		StringBuffer sb = new StringBuffer(sPassword);
		for (i=0;i<iLen;i++)
		{
//			c=aPassword[i];
			c = aPassword.charAt(i);
//			sPassword=sPassword.Remove(iOff-1,1);
			sb.deleteCharAt(iOff-1);
//			sPassword=sPassword.Insert(iOff-1,c.ToString());
			sb.insert(iOff-1,c);
//			iOff+=3;
			iOff+=3;
		}
		sPassword = sb.toString();
		return sPassword;
	}
	
	
	/**
	 * 新中大 解密算法
	 * 
	 * 解密用户密码的时候采用128位加密，加密url登入串的时候1024位加密
	 * 
	 * 如果密码长度不等于128位或者1024位，就不进行解密，返回原来的串
	 * @param aPassword
	 * @param aLen
	 * @return
	 */
	public static String decodeStringNG(String aPassword, int aLen){ 
	
		String sPassword, s, sCaption;
		int iOff, i, iLen;
		if (aPassword == null)
			aPassword = "";
//		if (aPassword.Length != aLen)
		if(aPassword.length() != aLen)
			return aPassword;

		// 得口令长度、大小写
//		iLen = aPassword.LastIndexOf('a') + 1;
		iLen = aPassword.lastIndexOf('a')+1;
//		sCaption = aPassword.Substring(iLen);
		sCaption = aPassword.substring(iLen);

		// 偏移
//		iOff = System.Convert.ToInt32(aPassword[0]) - 40;
		iOff = (int)(aPassword.charAt(0))-40;
		
		StringBuffer aPasswordBuffer = new StringBuffer(aPassword);
		// 得口令
		sPassword = "";
		for (i = 0; i < aLen - iLen; i++) {
//			s = aPassword.Substring(iOff - 1, 1);
			s = aPassword.substring(iOff-1,iOff);
//			sPassword += s;
			sPassword += s;
//			iOff += 3;
			iOff +=3;
		}
		StringBuffer sPasswordBuffer = new StringBuffer(sPassword);
		// 处理口令大小写
		StringBuffer sCaptionBuffer = new StringBuffer(sCaption);
		for (i = 0; i < aLen - iLen; i++) {
//			s = sCaption.Substring(i, 1);
			 s = sCaption.substring(i,i+1);
//			if (s == "1") {
			 if(s.equals('1')){
//				s = sPassword[i].ToString();
				s = Character.toString(sPasswordBuffer.charAt(i));
//				sPassword = sPassword.Remove(i, 1);
				sPasswordBuffer.deleteCharAt(i);
//				sPassword = sPassword.Insert(i, s.ToUpper());
				sPasswordBuffer.insert(i,s.toUpperCase());
			}
		}
		return sPassword;
	}
	
	public static void main(String[] args){
//		System.out.println(EncodeString("www.myurp.com",128));
//		for(int i=0;i<1000;i++){
			String str = EncodeStringNG("exe%sdfec&sdf((*@#fermanager",1024);
			String tempStr = "agdrjw6as4dlbwc1w5Ap2mu4itc/rtosbcalrepwlipds94tyMdfijicdjmb9rst9edgr.uvBye39ryj=0c1er0ih.7M05g.051mi5t9.i$8e$7fe$ew@u0$n&a0dcsdclfo0cux8t1$=te0g80t90sy1aw$sy@e2$r6o7lci$owodnaess=iv03b04w0q11&x.kr0ht1ua$mb@7m$a$l8hotugykiiydtw=yv0co0610rm0ou68c$bb@v1$30p2rwt0dra=kb$@m@t5$aqltgatlnh4gy7u61a.iggsev5=wdz8vh$n-6qc1nni$$$5@c5$8ehbginfduamioaqtiqpnr=f22o8orhkmfn=vf0$s$w0@9g$urofhtfgh7=e0ir0pp-eartrxcac0m0w=68$j0@nt$nxf@9utmnsnthdi=0oernpsuyurv=l44=htwjuelnbgumkaapnibelcr/p2mLxag5ia1lgva7sdo4dvdriglMli9esqat6c.Mja3lscaphjxai?2ja5ms=dbvaadjc4.knbpggaiegL0ekf=1nmavapxi13l$rl8yietsn8tig.fkayesnyp3ixe0&cbmsoowjd@iu34ltkeil=n2dfsmmfcei2mtmtc4@kvr08fjs64tse0sLm9s0L8w3it89ajoaqol6pi0l3q12honiykinkg9@0h9bd2r26u$97e$sjkio4=pcxdkw7p3jt67stoanpeq2s8e5el2hvosg51ugiqe07l.4gjgfcdi12e@ewcirlxpmvmjegilcreahelfgevcga87r2qtyirsx9om2nd3$8gw7q66a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001000010001000100000000000100010000100010000000000000000000";
			System.out.println(str);
			System.out.println(decodeStringNG(tempStr,1024));
//		}
	}
}
